var RPCClient = require('@alicloud/pop-core').RPCClient
var express = require('express')
var router = express.Router()

function initVodClient(accessKeyId, secretAccessKey) {
  var regionId = 'cn-shanghai' // 点播服务接入区域
  var client = new RPCClient({
    accessKeyId: accessKeyId,
    secretAccessKey: secretAccessKey,
    endpoint: 'http://vod.' + regionId + '.aliyuncs.com',
    apiVersion: '2017-03-21'
  })
  return client
}

let AccessKeyId = '******'
let AccessKeySecret = '******'

let client = initVodClient(AccessKeyId, AccessKeySecret)

// http://localhost:3000/vod/videoPlayAuth?VideoId=1b2242b2902f4ad98293009a0a5495f5

router.get('/videoPlayAuth', function(req, res, next) {

  console.log(req.query)

  var { VideoId } = req.query
  
  client.request('GetVideoPlayAuth',{ VideoId }, {}).then(function(response) {
    // play auth
    console.log('PlayAuth = ' + response.PlayAuth)
    // base metadata
    if (response.VideoMeta) {
      console.log('VideoMeta.Title = ' + response.VideoMeta.Title)
    }
    console.log('RequestId = ' + response.RequestId)

    res.send(response)
  }).catch(function(response) {
    console.log('ErrorCode = ' + response.data.Code)
    console.log('ErrorMessage = ' + response.data.Message)
    console.log('RequestId = ' + response.data.RequestId)
  })

  // res.send({ VideoId })
  
})

module.exports = router
